package org.ami.onlineschool.ui;

import java.awt.EventQueue;

import javax.swing.JFrame;
import java.awt.BorderLayout;
import java.io.File;

import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import org.ami.onlineschool.dataAccess.SchoolDataAccess;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class UIDefineCourses extends AbstractWorkWindow {

	private JFrame frmSchool;
	private JTextField txtRepositoryPath;
	private CourseDashboard dashboard;
	private JPanel panel_1;
	private JButton btnSave;

	/**
	 * Launch the application.
	 * @throws UnsupportedLookAndFeelException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 * @throws ClassNotFoundException 
	 * @wbp.parser.entryPoint
	 */
	public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
		
		UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		
		launchWindow(new File("").getAbsolutePath());
	}

	static void launchWindow(final String dataLocation) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					UIDefineCourses window = new UIDefineCourses();
					window.setSchoolDataLocation(dataLocation);
					window.initialize();
					window.frmSchool.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public UIDefineCourses() {
		//initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		
		
		
		frmSchool = new JFrame();
		frmSchool.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				dashboard.saveSchoolOnDisk();
			}
		});
		frmSchool.setTitle("School");
		frmSchool.setBounds(100, 100, 585, 452);
		//frmSchool.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		dashboard = new CourseDashboard();
		//dashboard.setSchoolController(new SchoolModel("C:\\Work\\school"));
		frmSchool.getContentPane().add(dashboard, BorderLayout.CENTER);
		
		JPanel panel = new JPanel();
		frmSchool.getContentPane().add(panel, BorderLayout.NORTH);
		panel.setLayout(new BorderLayout(0, 0));
		
		txtRepositoryPath = new JTextField();
		txtRepositoryPath.setEnabled(false);
		txtRepositoryPath.setText(getSchoolDataLocation());
		panel.add(txtRepositoryPath, BorderLayout.CENTER);
		txtRepositoryPath.setColumns(10);
		
		panel_1 = new JPanel();
		panel.add(panel_1, BorderLayout.EAST);
		
		btnSave = new JButton("Save");
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dashboard.saveSchoolOnDisk();
			}
		});
		panel_1.add(btnSave);
		
		loadSchoolData();
	}

	protected void loadSchoolData() {
		
		dashboard.setSchoolModel(new SchoolDataAccess(txtRepositoryPath.getText()));
		
	}
}
